// 入口文件
// 1.1 引入vue
import Vue from 'vue'
// 1.2 导入路由
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

//1.3 导入自己的router.js 路由模块
import router from './router.js'

// 引入测试的本地json 文件
require('./mock/index.js')

// 导入mui的css样式
import './lib/mui/css/mui.min.css'
//导入mui扩展
import './lib/mui/css/icons-extra.css'

//按需导入mint-ui中的组件
/* import { Header, Swipe, SwipeItem, Button,Lazyload } from 'mint-ui'
Vue.component(Header.name, Header)
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Button.name, Button);
Vue.use(Lazyload); */
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

//2.1 导入 vue-resource
import VueResource from 'vue-resource'
//2.2 安装 vue-resource
Vue.use(VueResource)
// 设置请求的根路径
Vue.http.options.root = 'http://www.liulongbin.top:3005'
// 全局设置 post 时候表单数据格式得组织形式 application/x-www-form-urlencoded
Vue.http.options.emulateJSON = true;

//导入Toast 
import { MessageBox } from "mint-ui";

// 注册 vuex
import Vuex from 'vuex'
Vue.use(Vuex)

let defaultCity = '北京'
let defaultCity1 = '上海'
let defaultCarInfo = {}
//let defaultCarDisplacement = ''
var car = JSON.parse(localStorage.getItem('car') || '[]')

try {
    if (localStorage.city) {
        defaultCity = localStorage.city
    }
} catch (e) { e }
try {
    if (localStorage.city1) {
        defaultCity1 = localStorage.city1
    }
} catch (e) { e }
try {
    if (localStorage.carInfo) {
        defaultCarInfo = localStorage.carInfo
    }
} catch (e) { e }
// try {
//     if (localStorage.carDisplacement) {
//         defaultCarDisplacement = localStorage.carDisplacement
//     }
// } catch (e) { e }

var store = new Vuex.Store({
    state: {
        city: defaultCity,
        city1: defaultCity1,
        carInfo: defaultCarInfo,
        car: car,
        changeCarInfo: {},
        index: '',
        service: '',
        services: {},
        choosedServices: [],
        choosedServicesName: [],
        num: 0
        // carDisplacement: defaultCarDisplacement
    },
    mutations: {
        changeCity(state, city) {
            state.city = city
            try {
                localStorage.city = city
            } catch (e) { e }
        },
        changeCity1(state, city) {
            state.city1 = city
            try {
                localStorage.city1 = city
            } catch (e) { e }
        },
        compareCity(state) {
            if (state.city !== state.city1) {
                MessageBox.confirm('', {
                    title: '提示',
                    message: '定位城市与默认城市不一致，是否需要切换',
                    confirmButtonText: '切换',
                    cancelButtonText: '取消'
                }).then(action => {
                    //console.log(123)
                    if (action == 'confirm') {
                        //console.log(state.city)
                        //console.log(state.city1)
                        // console.log(localStorage.state.city)
                        // console.log(localStorage.state.city1)
                        state.city = state.city1
                        localStorage.city = state.city1
                        //state.city = ''
                    }
                }).catch(error => {
                    if (error == 'cancel') {
                        //console.log('点击取消')
                    }
                })
            }
            // state.city = state.city1
            // localStorage.city = state.city1;
        },
        clearCurrentCity(state) {
            //this.city = localStorage.city = ''
        },
        getCarInfo(state, obj) {
            state.carInfo = obj
        },
        getCarDisplacement(state, key) {
            state.carInfo.displacement = key
        },
        getAddCarYear(state, item) {
            state.carInfo.year = item
            //console.log(state.carInfo)
        },
        getCar(state, key) {
            //循环遍历state中的car
            var flag = false;
            var index = state.index
            state.car.some(item => {
                if (item.name == key.name &&
                    item.carCc == key.carCc &&
                    item.displacement == key.displacement &&
                    item.year == key.year &&
                    JSON.stringify(item.carId) == JSON.stringify(key.carId) &&
                    item.url == key.url &&
                    item.imgActive == key.imgActive &&
                    item.buttonActive == key.buttonActive) {
                    flag = true;
                    return true;
                }
            })
            if (!flag) {
                if (index !== '') {
                    if (state.car[index].imgActive === true) {
                        key.imgActive = true;
                        key.buttonActive = false;
                        state.car.splice(index, 1, key)
                    } else {
                        state.car.splice(index, 1, key)
                    }

                } else {
                    if (state.car.length === 0) {
                        key.imgActive = true;
                        key.buttonActive = false;
                        state.car.unshift(key)
                    } else {
                        state.car.unshift(key)
                    }

                }
            }
            // if (!flag) {
            //     state.car.unshift(key)
            // }
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        delCarInfo(state, index) {
            if (state.car[index].imgActive && state.car.length > 1) {
                state.car.splice(index, 1)
                state.car[0].imgActive = true;
                state.car[0].buttonActive = false;
            } else {
                state.car.splice(index, 1)
            }
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        changeActive(state, index) {
            state.car.some(item => {
                item.imgActive = false;
                item.buttonActive = true;
            })
            state.car[index].imgActive = true;
            state.car[index].buttonActive = false;
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        getChangeCarInfo(state, index) {
            state.changeCarInfo = state.car[index];
            state.index = index
        },
        clearChangeCarInfo(state) {
            state.changeCarInfo = {}
        },
        getClickService(state, e) {
            state.service = e
        },
        getServicesNum(state, services) {
            var count = 0
            for (let i in services) {
                for (let n in services[i]) {
                    for (let index in services[i][n]) {
                        if (services[i][n][index].flag === 'true') {
                            count = count + 1
                        }
                    }
                }
            }
            state.num = count
            state.services = services
        },
        getChoosedServices(state) {
            var arr = []
            var arr1 = []
            var arr2 = []
            for (let i in state.services) {
                for (let n in state.services[i]) {
                    for (let index in state.services[i][n]) {
                        if (state.services[i][n][index].flag === 'true') {
                            arr1.push(i)
                            arr.push(state.services[i][n][index])
                        }
                    }
                }
            }
            state.choosedServices = arr

            //遍历第一个包含重复名字的数组，如果第二个数组内没有与第一个数组名相同的，则把名字放到第二个数组
            for (let i in arr1) {
                if (arr2.indexOf(arr1[i]) === -1) {
                    arr2.push(arr1[i])
                }
            }
            state.choosedServicesName = arr2
            //console.log(state.choosedServicesName)
        },
        getKeyword(state, key) {
        }
    },
    getters: {

    }
})

//导入app组件
import app from './APP.vue'

//创建vue 实例对象
var vm = new Vue({
    el: '#app',
    render: c => c(app),
    router,
    store // 挂载vuex
})


